package com.mes.common.utils.poi;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;

public class CustomCellWriteHandler extends AbstractCellStyleStrategy implements CellWriteHandler {
    private XSSFCellStyle headCellStyle;
    private XSSFCellStyle contentCellStyle;
    @Override
    protected void initCellStyle(Workbook workbook) {
        headCellStyle = (XSSFCellStyle) StyleUtil.buildHeadCellStyle(workbook, null);
        headCellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 128, 128)));
        headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font font = workbook.createFont();
        font.setFontName("Arial");
        font.setFontHeightInPoints((short) 10);
        font.setBold(true);
        byte[] rgb = new byte[]{(byte) 255, (byte) 255, (byte) 255};
        XSSFFont xssfFont = (XSSFFont) font;
        xssfFont.setColor(new XSSFColor(rgb, null));
        headCellStyle.setAlignment(HorizontalAlignment.CENTER);
        headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headCellStyle.setBorderRight(BorderStyle.THIN);
        headCellStyle.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        headCellStyle.setBorderLeft(BorderStyle.THIN);
        headCellStyle.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        headCellStyle.setBorderTop(BorderStyle.THIN);
        headCellStyle.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        headCellStyle.setBorderBottom(BorderStyle.THIN);
        headCellStyle.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        headCellStyle.setFont(font);

        contentCellStyle = (XSSFCellStyle) StyleUtil.buildContentCellStyle(workbook, null);
        Font contentFont = workbook.createFont();
        contentFont.setFontName("Arial");
        contentFont.setFontHeightInPoints((short) 10);
        contentCellStyle.setAlignment(HorizontalAlignment.CENTER);
        contentCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        contentCellStyle.setBorderRight(BorderStyle.THIN);
        contentCellStyle.setRightBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
        contentCellStyle.setBorderLeft(BorderStyle.THIN);
        contentCellStyle.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
        contentCellStyle.setBorderTop(BorderStyle.THIN);
        contentCellStyle.setTopBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
        contentCellStyle.setBorderBottom(BorderStyle.THIN);
        contentCellStyle.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
        contentCellStyle.setFont(contentFont);

    }

    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        cell.setCellStyle(headCellStyle);
    }

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        cell.setCellStyle(contentCellStyle);
    }
}
